package com.example.firstboot.controller;



import com.alibaba.fastjson.JSONObject;
import com.example.firstboot.entity.Admin;
import com.example.firstboot.entity.TbCheck;
import com.example.firstboot.entity.TbUser;
import com.example.firstboot.service.AdminService;
import com.example.firstboot.service.SequenceService;
import com.example.firstboot.service.TbUserService;
import com.example.firstboot.util.JsonResult;
import com.example.firstboot.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private Admin admin;

    @Autowired
    private AdminService adminService;

    @RequestMapping("/test")
    @ResponseBody
    public TbUser test() {
        TbUser tbUser = tbUserService.selectByPrimaryKey(1);
        return tbUser;
    }

    @RequestMapping("/test1")
    @ResponseBody
    public TbCheck test1() {
        TbCheck tbCheck = adminService.selectByPrimaryKey(1);
        return tbCheck;
    }

    @RequestMapping("/findAllCheck")
    @ResponseBody
    public JsonResult<List<TbCheck>> findAllCheck() {
        List<TbCheck> tbCheck = adminService.findAllCheck();
        if (tbCheck != null) {
            return new JsonResult<>(tbCheck, "返回数据成功");
        } else {
            return new JsonResult<>(-1, "返回数据失败");
        }
    }

    @RequestMapping("/findAllMedicine")
    @ResponseBody
    public JsonResult<List<TbCheck>> findAllMedicine() {
        List<TbCheck> tbCheck = adminService.findAllMedicine();
        if (tbCheck != null) {
            return new JsonResult<>(tbCheck, "返回数据成功");
        } else {
            return new JsonResult<>(-1, "返回数据失败");
        }
    }


    @PostMapping("/checkUserLogin")
    @ResponseBody
    public JsonResult<TbUser> checkUserLogin(@RequestBody JSONObject jsonObject) {
        String account = String.valueOf(jsonObject.get("account"));
        String password = String.valueOf(jsonObject.get("password"));
        TbUser tbUser = tbUserService.selectByUserAccount(account);
//        System.out.println(1111111);
        if (tbUser == null || !tbUser.getPassword().equals(MD5Utils.getMD5(password))) {
            return new JsonResult<TbUser>(-1, "用户名或密码错误");
        } else {
            return new JsonResult<TbUser>(tbUser);
        }
    }

    @RequestMapping("/registerUser")
    @ResponseBody
    public JsonResult registerForUser(@RequestBody JSONObject jsonObject) {
        String username = String.valueOf(jsonObject.get("username"));
        String password = String.valueOf(jsonObject.get("password"));
        String roleFlag = String.valueOf(jsonObject.get("roleFlag"));
        System.out.println(username);
        System.out.println(password);
        System.out.println(roleFlag);
        String account = "";
        switch (roleFlag) {
            case "1": {
                account = sequenceService.nextValue("mag_seq", 1);
                break;
            }
            case "2": {
                account = sequenceService.nextValue("doc_seq", 2);
                break;
            }
            case "3": {
                account = sequenceService.nextValue("pat_seq", 3);
                break;
            }
        }
        Integer row = tbUserService.createAccount(account, username, MD5Utils.getMD5(password), Integer.valueOf(roleFlag));
        if (row.equals(0)) {
            return new JsonResult(-1, "创建账号失败");
        } else if (row.equals(1)) {
            return new JsonResult(account, "创建账号成功");
        }
        return new JsonResult(-2, "数据库状态异常");
    }

    /*获取管理员密码*/
    @RequestMapping("/getAdminPassword")
    @ResponseBody
    public JsonResult<String> getAdminPassword() {
        return new JsonResult<String>(admin.getAdminPassword(), 1116, "获取管理员密码成功");
    }

    @RequestMapping("/findUserByAccount")
    @ResponseBody
    public JsonResult<TbUser> findUserByAccount(@RequestBody JSONObject jsonObject) {
        String account = String.valueOf(jsonObject.get("account"));
        TbUser tbUser = tbUserService.selectByUserAccount(account);
        System.out.println(tbUser);
        if (tbUser != null) {
            return new JsonResult<TbUser>(tbUser, "获取用户信息成功");
        } else {
            return new JsonResult<TbUser>(-1, "获取用户信息失败");

        }
    }

//    @RequestMapping("/findUserForReset")
//    @ResponseBody
//    public JsonResult<TbUser> findUserForReset(@RequestBody JSONObject jsonObject) {
//        String account = String.valueOf(jsonObject.get("account"));
//        TbUser tbUser = tbUserService.selectByUserAccount(account);
//        System.out.println(tbUser);
//        return new JsonResult<TbUser>(tbUser, "获取用户信息成功");
//    }

    @RequestMapping("/findAmount")
    @ResponseBody
    public JsonResult findAmount(@RequestBody JSONObject jsonObject) {
        String roleflag = String.valueOf(jsonObject.get("roleflag"));
        Integer amount = tbUserService.selectCountUserAmount(roleflag);
        if (amount != 0) {
            return new JsonResult(amount, "获取用户数量成功");
        } else {
            return new JsonResult(-1, "获取用户数量失败");
        }
    }

    @RequestMapping("/addCheck")
    @ResponseBody
    public JsonResult addCheck(@RequestBody JSONObject jsonObject) {
        String name = String.valueOf(jsonObject.get("name"));
        String price = String.valueOf(jsonObject.get("price"));
        Integer row = adminService.addCheck(name,price);
        if (row != null) {
            return new JsonResult(0, "插入成功");
        }else {
            return new JsonResult(-1, "插入失败");
        }
    }

    @RequestMapping("/addMedicine")
    @ResponseBody
    public JsonResult addMedicine(@RequestBody JSONObject jsonObject) {
        String name = String.valueOf(jsonObject.get("name"));
        String price = String.valueOf(jsonObject.get("price"));
        Integer row = adminService.addMedicine(name,price);
        if (row != null) {
            return new JsonResult(0, "插入成功");
        }else {
            return new JsonResult(-1, "插入失败");
        }
    }

    @RequestMapping("/alertCheck")
    @ResponseBody
    public JsonResult alertCheck(@RequestBody JSONObject jsonObject) {
        String id = String.valueOf(jsonObject.get("id"));
        String name = String.valueOf(jsonObject.get("name"));
        String price = String.valueOf(jsonObject.get("price"));
        Integer row = adminService.alertCheck(id,name,price);
        if (row != null) {
            return new JsonResult(0, "插入成功");
        }else {
            return new JsonResult(-1, "插入失败");
        }
    }

    @RequestMapping("/alertMedicine")
    @ResponseBody
    public JsonResult alertMedicine(@RequestBody JSONObject jsonObject) {
        String id = String.valueOf(jsonObject.get("id"));
        String name = String.valueOf(jsonObject.get("name"));
        String price = String.valueOf(jsonObject.get("price"));
        Integer row = adminService.alertMedicine(id,name,price);
        if (row != null) {
            return new JsonResult(0, "插入成功");
        }else {
            return new JsonResult(-1, "插入失败");
        }
    }

    @RequestMapping("/deleteCheck")
    @ResponseBody
    public JsonResult deleteCheck(@RequestBody JSONObject jsonObject) {
        String id = String.valueOf(jsonObject.get("id"));
        Integer row = adminService.deleteCheck(id);
        if (row != null) {
            return new JsonResult(0, "插入成功");
        }else {
            return new JsonResult(-1, "插入失败");
        }
    }

    @RequestMapping("/deleteMedicine")
    @ResponseBody
    public JsonResult deleteMedicine(@RequestBody JSONObject jsonObject) {
        String id = String.valueOf(jsonObject.get("id"));
        Integer row = adminService.deleteMedicine(id);
        if (row != null) {
            return new JsonResult(0, "插入成功");
        }else {
            return new JsonResult(-1, "插入失败");
        }
    }
}
